/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2007 by Sunplus mMedia Inc.                      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus        *
 *  mMedia Inc. All rights are reserved by Sunplus mMedia Inc.  This      *
 *  software may only be used in accordance with the corresponding        *
 *  license agreement. Any unauthorized use, duplication, distribution,   *
 *  or disclosure of this software is expressly forbidden.                *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Sunplus mMedia Inc.                                *
 *                                                                        *
 *  Sunplus mMedia Inc. reserves the right to modify this software        *
 *  without notice.                                                       *
 *                                                                        *
 *  Sunplus mMedia Inc.                                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 **************************************************************************/

#ifndef __INITIO_H__
#define __INITIO_H__

#include "general.h"
#include "sysMemInit.h"
/* ------ Note: Demo code zone start flag, don't modify or delete this line!!! ------ */
#define  SUPPORT_D1
/* ------ Note: Demo code zone end flag, don't modify or delete this line!!! ------ */
//------------------------------------------------------------------------------
//Constant
//------------------------------------------------------------------------------
#define CodeInDRAM_Option	1
//***************************************************************************
//All SDRAM Constant Are Defined in Unit of WORD
//***************************************************************************

//Number of Character font
#define K_CHARFONT_NumChar              ((UINT32)768)

//Resolution of Graphic font
#define K_GRAFFONT_Width                ((UINT32)256)
#define K_GRAFFONT_Height               ((UINT32)256)

//Resolution of Video clip
#ifdef SUPPORT_D1 //xian
#define K_VIDEOCLIP_Width               ((UINT32)720)
#define K_VIDEOCLIP_Height              ((UINT32)480)
#else
#define K_VIDEOCLIP_Width               ((UINT32)640)
#define K_VIDEOCLIP_Height              ((UINT32)480)
#endif

#define K_Preview_Width               ((UINT32)320) //((UINT32)720)
#define K_Preview_Height              ((UINT32)240) //((UINT32)480)

//Size of total SDRAM = 16M word = 32M byte
//16M word = 0x00000000 - 0x00ffffff word

#define K_SDRAM_TotalSize               sysMemTotSizeGet()

/* Allen added for awbctt.bin buf address */
#ifdef LOAD_AWBCTT_BIN
#define K_SDRAM_AWBCTT_Buf_Address		((sysMemAddrGet(MEM_IDX_DATA) << 1) + 0x8000 + (0x7400 - 0x4000)) /* mapping to xdata 0x7400 */
#endif

//***************************************************************************
//Buffer Size
//***************************************************************************
//*************************************
//Common
//*************************************
//code size
#if(CodeInDRAM_Option)
#define K_SDRAM_CodeSize 		((UINT32)256 * (UINT32)1024) //512KB
#else
#define K_SDRAM_CodeSize 		((UINT32)0)
#endif

/*
	        Low Addr
	 -------------------------                         
    | 0x8000 ~ 0xffff mapping |
     -------------------------
    | 0x4000 ~ 0x7fff mapping |
     -------------------------
            High Addr
*/

#define K_SDRAM_ShadowSize		((UINT32)(8 + 16) * (UINT32)1024) //48KB

//Size of Bad pixel coordinate buffer
#define K_SDRAM_BadPixel_PrvBufSize		((UINT32)8 * (UINT32)1024) //16KB
#define K_SDRAM_BadPixel_CapBufSize		((UINT32)16 * (UINT32)1024) //32KB
#define K_SDRAM_BadPixel_CapScaleBufSize	((UINT32)32 * (UINT32)1024) //64KB
#define K_SDRAM_BadPixel_DummyBufSize		((UINT32)6 * (UINT32)1024) //12KB reserved, compatible for old buffer map

#define K_SDRAM_BadPixelCoordBufSize \
	(K_SDRAM_BadPixel_PrvBufSize + K_SDRAM_BadPixel_CapBufSize + K_SDRAM_BadPixel_DummyBufSize)

//Size of Osd Display buffer
#define K_SDRAM_OsdDisplayBufSize       ((UINT32)0x00004000)
#define K_SDRAM_OsdHiddenBufSize        ((UINT32)0x00004000)

//Size of Character font buffer
#define K_SDRAM_CharFontBufSize         (K_CHARFONT_NumChar * 64)

//Size of Graphic font buffer
#define K_SDRAM_GrafFontBufSize         (K_GRAFFONT_Width * K_GRAFFONT_Height + 4096 + 2048) // 4096 + 2048 is for DS which needs 70208 word

//#define K_SDRAM_DOS_WorkSize            ((UINT32)0x00020000)     //128K word = 256 byte, at least dos cluster size
#define K_SDRAM_DOS_WorkSize            ((UINT32)0x00020000 - 0x4000)     //112K word = 224 byte, at least dos cluster size

//Size of DOS FAT1 buffer
#define K_SDRAM_DOS_Fat1BufSize         ((UINT32)0x00010000)     //64K word = 128K byte

//Size of DCF directory buffer
#define K_SDRAM_DcfDirBufSize           ((UINT32)0x0001FA4)      //director index 100 to 999 => 900 * 9 = 8100

//patch4.5@ada@Record DCF file name begin
#define K_SDRAM_JPG_FdbParameterBufSize ((UINT32)0x00018696) //10 * 9999
//#define K_SDRAM_JPG_FdbParameterBufSize ((UINT32)0x00013878) //8 * 9999

//#define K_SDRAM_AVI_FdbParameterBufSize ((UINT32)0x00018696) //10 * 9999
#define K_SDRAM_AVI_FdbParameterBufSize ((UINT32)0x00000000)
//patch4.5@ada@Record DCF file name end

#define K_SDRAM_WAV_FdbParameterBufSize ((UINT32)0x00000000)

#define K_SDRAM_KeySoundSize		((UINT32)2560)
#define K_SDRAM_PwrCtrlParameterBufSize		((UINT32)512)

#define K_SDRAM_DosRamBufSize		sysMemSizeGet(MEM_IDX_DOS_RAM)    //((UINT32)0)//NO DOS RAM

#ifdef ICAT_OPTION
#define K_SDRAM_IcatCdspParamBufSize	((UINT32)1024)
#define K_SDRAM_IcatGammaBufSize		((UINT32)1024)
#define K_SDRAM_IcatLcBufSize			((UINT32)1024)
#endif

//patch5.0@richie@SDRAM begin
//*************************************
//Preview Capture
//*************************************
//Size of Preview Buffer A
#define K_SDRAM_PreviewBufSizeA        (K_Preview_Width * K_Preview_Height)

//Size of Preview Buffer B
#define K_SDRAM_PreviewBufSizeB        (K_Preview_Width * K_Preview_Height)

//Size of Preview Buffer C
#define K_SDRAM_PreviewBufSizeC        (K_Preview_Width * K_Preview_Height)

//Size of Preview Buffer D
#define K_SDRAM_PreviewBufSizeD        (K_Preview_Width * K_Preview_Height)
//patch5.0@richie@SDRAM end

//*************************************
//Single Capture
//*************************************

//*************************************
//Preview/AVI Capture
//*************************************
#define K_SDRAM_AviPvwBufSizeA			(K_VIDEOCLIP_Width * K_VIDEOCLIP_Height)//(K_VIDEOCLIP_Width * (K_SENSOR_MonitorHeight - 13))
#define K_SDRAM_AviPvwBufSizeB			(K_VIDEOCLIP_Width * K_VIDEOCLIP_Height)//(K_VIDEOCLIP_Width * (K_SENSOR_MonitorHeight - 13))
#define K_SDRAM_AviPvwBufSizeC			(K_VIDEOCLIP_Width * K_VIDEOCLIP_Height)//(K_VIDEOCLIP_Width * (K_SENSOR_MonitorHeight - 13))
#define K_SDRAM_AviPvwBufSizeD			(K_VIDEOCLIP_Width * K_VIDEOCLIP_Height)//(K_VIDEOCLIP_Width * (K_SENSOR_MonitorHeight - 13))

#define K_SDRAM_AviCapVLCBufSizeA		((K_VIDEOCLIP_Width * K_VIDEOCLIP_Height / 4) + 130 + 6)    // +6 to keep the aligment of 8
#define K_SDRAM_AviCapVLCBufSizeB		((K_VIDEOCLIP_Width * K_VIDEOCLIP_Height / 4) + 130 + 6)
#define K_SDRAM_AviCapVLCBufSizeC		((K_VIDEOCLIP_Width * K_VIDEOCLIP_Height / 4) + 130 + 6)
#define K_SDRAM_AviCapVLCBufSizeD		((K_VIDEOCLIP_Width * K_VIDEOCLIP_Height / 4) + 130 + 6)
//patch5.2.1@ada@Fix AVI recording 448x336 frame corrupt end
#define K_SDRAM_AviCapAudioBufSize		((UINT32)32 * (UINT32)1024)

#define K_SDRAM_AviCapFragBufSizeA		((UINT32)32 * (UINT32)1024 * (UINT32)4 + (K_VIDEOCLIP_Width * K_VIDEOCLIP_Height)/4)

#define K_SDRAM_AviCapHeaderBufSize		((UINT32)256)
//#define K_SDRAM_AviCaptureIdx1Size		(remain size)

#define K_SDRAM_AviCapQTabNum           10
#define K_SDRAM_AviCapQTabSize          ((UINT32)65*K_SDRAM_AviCapQTabNum)

//*************************************
//Playback 1/4/9
//*************************************

#define  K_SDRAM_FileOrderTableSize				((UINT32)9999)

#define SP1K_PB_DISP_BUF_SIZE_A		((UINT32)320 * (UINT32)240)
#define SP1K_PB_DISP_BUF_SIZE_B		((UINT32)320 * (UINT32)240)
#define K_SDRAM_ImagePlaybackDisplayBufSize	(SP1K_PB_DISP_BUF_SIZE_A + SP1K_PB_DISP_BUF_SIZE_B)

#define K_SDRAM_ImagePlaybackBufSize			((UINT32)640 * (UINT32)480)

//*************************************
//Playback AVI
//*************************************
#define K_SDRAM_AviPbBufSizeA		((UINT32)K_VIDEOCLIP_Width * K_VIDEOCLIP_Height) //((UINT32)320 * (UINT32)240)
#define K_SDRAM_AviPbBufSizeB		((UINT32)K_VIDEOCLIP_Width * K_VIDEOCLIP_Height) //((UINT32)320 * (UINT32)240)
#define K_SDRAM_AviPbBufSizeC		((UINT32)K_VIDEOCLIP_Width * K_VIDEOCLIP_Height) //((UINT32)320 * (UINT32)240)
#define K_SDRAM_AviPbFragSize		((UINT32)512 * (UINT32)1024+128*(UINT32)1024)
#define K_SDRAM_AviPbVideoBufSize	((UINT32)(0) )//320 * 240 / 4 + 256 + 256
#define K_SDRAM_AviPbAudioBufSize	((UINT32)64 * (UINT32)1024)//((UINT32)4096)
#define K_SDRAM_AviPbAudioChunkBufSize	((UINT32)16 * (UINT32)1024 + (UINT32)512)
//#define K_SDRAM_AviPbIdx1BufSize	(remain size)



//*************************************
//DPS
//*************************************
#define K_SDRAM_DPSBufSize 	((UINT32)8UL*1024UL)

//***************************************************************************
//Buffer Address
//***************************************************************************
//*************************************
//Common
//*************************************
#define K_SDRAM_CodeAddr		sysMemAddrGet(MEM_IDX_TEXT)
#define K_SDRAM_ShadowAddr		sysMemAddrGet(MEM_IDX_DATA)

#define K_SDRAM_BadPixelCoordBufAddr    sysMemAddrGet(MEM_IDX_BP)
#define K_SDRAM_OsdDisplayBufAddr       sysMemAddrGet(MEM_IDX_OSD_DISP)
#define K_SDRAM_OsdHiddenBufAddr        sysMemAddrGet(MEM_IDX_OSD_HIDDEN)
#define K_SDRAM_CharFontBufAddr         sysMemAddrGet(MEM_IDX_CHAR_FONT)
#define K_SDRAM_GrafFontBufAddr         sysMemAddrGet(MEM_IDX_GRAF_FONT)
#define K_SDRAM_DOS_WorkAddr            sysMemAddrGet(MEM_IDX_DOS_WORK)
#define K_SDRAM_DOS_FAT1Addr            sysMemAddrGet(MEM_IDX_DOS_FAT1)
#define K_SDRAM_DcfDirBufAddr           sysMemAddrGet(MEM_IDX_DCF_DIR)
#define K_SDRAM_JPG_FdbParameterBufAddr sysMemAddrGet(MEM_IDX_FDB)
#define K_SDRAM_AVI_FdbParameterBufAddr sysMemAddrGet(MEM_IDX_FDB)
#define K_SDRAM_WAV_FdbParameterBufAddr sysMemAddrGet(MEM_IDX_FDB)
#define K_SDRAM_FileOrderTableAddr      sysMemAddrGet(MEM_IDX_FILE_ORDER_TAB)
#define K_SDRAM_KeySoundAddr            sysMemAddrGet(MEM_IDX_KEY_SOUND)
#define K_SDRAM_PwrCtrlParameterBuf     sysMemAddrGet(MEM_IDX_PWR_CTRL_PARAM)
#define K_SDRAM_DosRamStartAddr         sysMemAddrGet(MEM_IDX_DOS_RAM)
#define K_SDRAM_DPSStartAddr         	sysMemAddrGet(MEM_IDX_DPS)

#ifdef ICAT_OPTION
#define K_SDRAM_IcatCdspParamBuf        sysMemAddrGet(MEM_IDX_ICAT_CDSP_PARAM)
#define K_SDRAM_IcatGammaBuf            sysMemAddrGet(MEM_IDX_ICAT_GAMMA)
#define K_SDRAM_IcatLcBuf               sysMemAddrGet(MEM_IDX_ICAT_LC)
#define K_SDRAM_CommonEndAddr           sysMemAddrGet(MEM_IDX_COMMON_END)
#else
#define K_SDRAM_CommonEndAddr           sysMemAddrGet(MEM_IDX_COMMON_END)
#endif

//*************************************
//Preview Capture
//*************************************
#define pv_AWBwndAddr					sysMemAddrGet(MEM_IDX_STILL_VIEW_AWB_WIN)
#define pv_AEwndAddr					sysMemAddrGet(MEM_IDX_STILL_VIEW_AE_WIN)
#define K_SDRAM_PreviewBufAddrA    		sysMemAddrGet(MEM_IDX_STILL_VIEW_PV_A)
#define K_SDRAM_PreviewBufAddrB   		sysMemAddrGet(MEM_IDX_STILL_VIEW_PV_B)
#define K_SDRAM_PreviewBufAddrC   		sysMemAddrGet(MEM_IDX_STILL_VIEW_PV_C)
#define K_SDRAM_PreviewBufAddrD  		sysMemAddrGet(MEM_IDX_STILL_VIEW_PV_D)

#ifdef ICAT_OPTION
#define icat_TestBuf_SIZE 4096
#define icat_TestBufAddr                sysMemAddrGet(MEM_IDX_ICAT_TEST)
#endif

//*************************************
//Preview/AVI Capture
//*************************************

#define AWBwndAddr						sysMemAddrGet(MEM_IDX_VIDEO_VIEW_AWB_WIN)
#define AEwndAddr						sysMemAddrGet(MEM_IDX_VIDEO_VIEW_AE_WIN)
#define K_SDRAM_AviPvwBufAddrA			sysMemAddrGet(MEM_IDX_VIDEO_VIEW_PV_A)
#define K_SDRAM_AviPvwBufAddrB			sysMemAddrGet(MEM_IDX_VIDEO_VIEW_PV_B)
#define K_SDRAM_AviPvwBufAddrC			sysMemAddrGet(MEM_IDX_VIDEO_VIEW_PV_C)
#define K_SDRAM_AviPvwBufAddrD			sysMemAddrGet(MEM_IDX_VIDEO_VIEW_PV_D)

#define K_SDRAM_AviCapVLCBufAddrA		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_A)
#define K_SDRAM_AviCapVLCBufAddrB		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_B)
#define K_SDRAM_AviCapVLCBufAddrC		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_C)
#define K_SDRAM_AviCapVLCBufAddrD		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_D)
#define K_SDRAM_AviCapVLCBufAddrE		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_E)
#define K_SDRAM_AviCapVLCBufAddrF		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_F)
#define K_SDRAM_AviCapVLCBufAddrG		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_G)
#define K_SDRAM_AviCapVLCBufAddrH		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_H)
#define K_SDRAM_AviCapVLCBufAddrI		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_I)
#define K_SDRAM_AviCapVLCBufAddrJ		sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_J)

#define K_SDRAM_AviCapVLCBufAddrA_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_A)
#define K_SDRAM_AviCapVLCBufAddrB_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_B)
#define K_SDRAM_AviCapVLCBufAddrC_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_C)
#define K_SDRAM_AviCapVLCBufAddrD_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_D)
#define K_SDRAM_AviCapVLCBufAddrE_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_E)
#define K_SDRAM_AviCapVLCBufAddrF_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_F)
#define K_SDRAM_AviCapVLCBufAddrG_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_G)
#define K_SDRAM_AviCapVLCBufAddrH_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_H)
#define K_SDRAM_AviCapVLCBufAddrI_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_I)
#define K_SDRAM_AviCapVLCBufAddrJ_Intr		sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_J)

#define K_SDRAM_AviCapVLCBufAddrTEMP	sysMemAddrGet(MEM_IDX_VIDEO_REC_VLC_TMP)
#define K_SDRAM_AviCapVLCBufAddrTEMP_Intr	sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_VLC_TMP)
#define K_SDRAM_AviQTabAddr             sysMemAddrGet(MEM_IDX_VIDEO_REC_QTAB)
#define K_SDRAM_AviQTabAddr_Intr             sysMemAddrGet_Intr(MEM_IDX_VIDEO_REC_QTAB)
#define K_SDRAM_AviCapAudioBufAddr     	sysMemAddrGet(MEM_IDX_VIDEO_REC_AUDIO)

#define K_SDRAM_AviCapFragBufAddrA		sysMemAddrGet(MEM_IDX_VIDEO_REC_FRAG)

#define K_SDRAM_AviCapHeaderBufAddr		sysMemAddrGet(MEM_IDX_VIDEO_REC_HDR)
#define K_SDRAM_AviCaptureIdx1Addr		sysMemAddrGet(MEM_IDX_VIDEO_REC_IDX)

//*************************************
//Playback 1/4/9
//*************************************

#define K_SDRAM_ImagePlaybackDisplayBufAddr	sysMemAddrGet(MEM_IDX_STILL_PB_DISP)
#define K_SDRAM_ImagePlaybackBufAddr		sysMemAddrGet(MEM_IDX_STILL_PB_GEN_A)
#define K_SDRAM_ImagePlaybackGeneralBufAddr	sysMemAddrGet(MEM_IDX_STILL_PB_GEN_B)

//*************************************
//Playback AVI
//*************************************
#define K_SDRAM_AviPbBufAddrA		sysMemAddrGet(MEM_IDX_VIDEO_PB_GEN_A)
#define K_SDRAM_AviPbBufAddrB		sysMemAddrGet(MEM_IDX_VIDEO_PB_GEN_B)
#define K_SDRAM_AviPbBufAddrC		sysMemAddrGet(MEM_IDX_VIDEO_PB_GEN_C)
#define K_SDRAM_AviPbFragAddr		sysMemAddrGet(MEM_IDX_VIDEO_PB_FRAG)

#define K_SDRAM_AviPbVideoBufAddr	sysMemAddrGet(MEM_IDX_VIDEO_PB_VIDEO)
#define K_SDRAM_AviPbAudioBufAddr   	sysMemAddrGet(MEM_IDX_VIDEO_PB_AUDIO)
#define K_SDRAM_AviPbAudioChunkBufAddr	sysMemAddrGet(MEM_IDX_VIDEO_PB_AUDIO_CHUNK)
#define K_SDRAM_AviPbIdx1BufAddr	sysMemAddrGet(MEM_IDX_VIDEO_PB_IDX)

//*************************************
//Maximum number of AVI chuncks
//*************************************
#define K_MAX_AVI_CHUNCKS   (UINT32)( ((UINT32)K_SDRAM_AviPvwBufAddrD - K_SDRAM_AviCaptureIdx1Addr) / 8 )

//------------------------------------------------------------------------------
//External Link
//------------------------------------------------------------------------------
extern  xdata   UINT8    G_ucSensorType;
extern  xdata   UINT8    G_ucSensorSize;
extern  xdata   UINT8    G_ucDscIndex;
extern  xdata   UINT8    G_ucClipIndex;
extern  xdata   UINT8    G_ucPccameraIndex;     //cch
extern  xdata   UINT8    G_ucDscIndexMax;
extern  xdata   UINT8    G_ucClipIndexMax;
extern  xdata   UINT8    G_ucPccameraIndexMax;  //cch@10/15

extern UINT8 xdata bUsbPwr;
//------------------------------------------------------------------------------
//Function Prototype
//------------------------------------------------------------------------------
void INITIO_Initialize(void);
void INITIO_GlobalInitialize(void);
void INITIO_UsbInitialize(void);
void INITIO_CpuInitialize(void);

void INITIO_SoftwareReset(void);
void INITIO_SetCameraSize(void); //cch
void INITIO_GetCameraSizeIndex(UINT16 index); //cch
void INITIO_SetCameraSizeIndex(UINT16 index); //cch
void INITIO_GetProductInfo(void);
void INITIO_GetInformation(UINT16 infoType);

#endif /*__INITIO_H__*/

